﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectWebNhac.BUS;
using System.Data;
using ProjectWebNhac.DTO;

namespace ProjectWebNhac.GUI.Admin
{
    public partial class QL_Video : System.Web.UI.Page
    {
        TheLoaiBus tlBus = new TheLoaiBus();
        VideoBus vdBus = new VideoBus();
        CaSiBus csbus = new CaSiBus();
        DataTable dt = new DataTable();
        VideoDTO vddto = new VideoDTO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadTheLoaiNhac();
                LoadGirdviewVideo();
            }
        }


        private void LoadGirdviewVideo()
        {
            dt = vdBus.LoadDanhSachVideo();
            gvDSVideo.DataSource = dt;
            gvDSVideo.DataBind();
        }

        private void LoadTheLoaiNhac()
        {
            drTheLoaiNhac.DataSource = tlBus.LayTheLoaiNhac();
            drTheLoaiNhac.DataValueField = "MaTheLoai";
            drTheLoaiNhac.DataTextField = "TenTheLoai";
            drTheLoaiNhac.DataBind();
        }

        private void LoadTheloainhacEdit()
        {
            drTheLoaiNhacEdit.DataSource = tlBus.LayTheLoaiNhac();
            drTheLoaiNhacEdit.DataValueField = "MaTheLoai";
            drTheLoaiNhacEdit.DataTextField = "TenTheLoai";
            drTheLoaiNhacEdit.DataBind();
        }

        private void EnableAdd()
        {

            txtCaSi.Enabled = true;
            txtCaSi.Text = "";

            txtTenVideo.Text = "";
            txtTenVideo.Enabled = true;
            drTheLoaiNhac.Enabled = true;
            flVideo.Enabled = true;
            flAvatar.Enabled = true;
            lnkHuy.Visible = true;
            lnkThem.Text = "Lưu";
        }

        private void DisableAdd()
        {
            txtCaSi.Enabled = false;
            txtCaSi.Text = "";
            txtTenVideo.Text = "";
            txtTenVideo.Enabled = false;
            drTheLoaiNhac.Enabled = false;
            flVideo.Enabled = false;
            flAvatar.Enabled = false;
            lnkHuy.Visible = false;
            lnkThem.Text = "Thêm";
            txtLuotXem.Text = "";
        }

        protected void lnkThem_Click(object sender, EventArgs e)
        {
            if (lnkThem.Text.Equals("Thêm"))
            {
                EnableAdd();
            }
            else if (lnkThem.Text.Equals("Lưu"))
            {
                vddto.TenVideo = txtTenVideo.Text;
                string TenCaSi = txtCaSi.Text;
                vddto.TenCaSi = txtCaSi.Text;
                vddto.Timespan = txtTimespan.Text;
                vddto.MaTheLoai = Convert.ToInt16(drTheLoaiNhac.SelectedValue);

                if (flVideo.FileName != null)
                    vddto.LinkVideo = flVideo.FileName;
                else
                    vddto.LinkVideo = "";

                if (flAvatar.FileName != null)
                    vddto.AvatarClip = flAvatar.FileName;
                else
                    vddto.AvatarClip = "";
                
                if (flVideo.PostedFile.FileName.Length > 0)
                {
                    if (vdBus.ThemVideo(vddto))
                    {
                        try
                        {
                            vddto.LinkVideo = flVideo.PostedFile.FileName;
                            flVideo.SaveAs(Server.MapPath("~/Video/" + vddto.LinkVideo));
                            vddto.AvatarClip = flVideo.PostedFile.FileName;
                            flAvatar.SaveAs(Server.MapPath("~/HinhAnh/Video/" + vddto.AvatarClip));
                        }
                        catch (System.Exception ex)
                        {

                        }
                        DisableAdd();
                        LoadGirdviewVideo();
                    }


                }
                else
                {
                }

                
            }

        }

        protected void btnSaveEdit_Click(object sender, EventArgs e)
        {
            VideoDTO objVideo = (VideoDTO)Session["CurrentVideo"];
            objVideo.TenCaSi = txtCasiEdit.Text;
            objVideo.TenVideo = txtTenVideoEdit.Text;
            objVideo.Timespan = txtTimespanEdit.Text;
            objVideo.MaTheLoai = Convert.ToInt16(drTheLoaiNhacEdit.SelectedValue);
            if (vdBus.SuaVideo(objVideo))
            {
                DisableAdd();
                LoadGirdviewVideo();
            }
        }

        protected void gvDSVideo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                Label lbCaSi = (Label)e.Row.FindControl("lbCaSi");
                Label lbTrangThai = (Label)e.Row.FindControl("lbTrangThai");
                LinkButton lbDetail = (LinkButton)e.Row.FindControl("lbDetail");
                int MaVideo = Convert.ToInt16(lbDetail.Text);
                lbCaSi.Text = csbus.LayCaSiTheoVideo(MaVideo);
                if (lbTrangThai.Text == "True")
                {
                    lbTrangThai.Text = "Đang Mở";
                    lbTrangThai.CssClass = "badge badge-success";
                }
            }
        }

        protected void gvDSVideo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("ChiTiet"))
            {
                DisableAdd();
                int MaVideo = Convert.ToInt16(e.CommandArgument);
                dt = vdBus.LayChiTietVideo(MaVideo);
                txtCaSi.Text = csbus.LayCaSiTheoVideo(MaVideo);
                txtTenVideo.Text = dt.Rows[0]["TenVideo"].ToString();
                txtLuotXem.Text = dt.Rows[0]["LuotXem"].ToString();
                txtTimespan.Text = dt.Rows[0]["Timespan"].ToString();
                imgAvatarClip.ImageUrl = "../../HinhAnh/Video/" + dt.Rows[0]["AvatarClip"].ToString();
                drTheLoaiNhac.SelectedValue = dt.Rows[0]["MaTheLoai"].ToString();

                vddto.MaVideo = Convert.ToInt16(dt.Rows[0]["MaVideo"]);
                vddto.TenVideo = txtTenVideo.Text;
                vddto.TenCaSi = txtCaSi.Text;
                vddto.Timespan = txtTimespan.Text;
                Session["CurrentVideo"] = vddto;

                LoadTheloainhacEdit();
                divEdit.Visible = true;
                lnkThem.Visible = false;
                lnkHuy.Visible = true;
            }

            if (e.CommandName.Equals("Xóa"))
            {
                int MaVideo = Convert.ToInt16(e.CommandArgument);
                vdBus.XoaVideo(MaVideo);
                LoadTheLoaiNhac();
                LoadGirdviewVideo();
            }

        }
    }
}